package com.xc.h05.controller.wyl;

import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.User;
import com.xc.h05.service.wyl.UserService;
import com.xc.h05.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //登录
    @GetMapping("/login")
    public R login(@RequestParam String username,@RequestParam String password){
        int code = userService.login(username, password);
        if (code == 1){
            return R.ok("登录成功");
        }else if (code == -1){
            return R.fail("用户名不存在");
        }else{
            return R.fail("密码错误");
        }
    }

    @GetMapping("/loginwc") //微信小程序登录
    public R loginwc(@RequestParam String username,@RequestParam String password){
        User user = userService.loginwc(username, password);
        if (user.getCode() == 1){
            return R.ok(user);
        }else if (user.getCode() == -1){
            return R.fail("用户名不存在");
        }else{
            return R.fail("密码错误");
        }
    }
    @GetMapping("/user")
    public R findAll(Integer pageNum, Integer pageSize){
        return R.ok(userService.findAll(pageNum, pageSize));

    }
    @PostMapping("/add")
    public R add(@RequestBody User user){
        int i = userService.insert(user);
        if (i>0){
            return R.ok(user); // wc修改
        }else {
            return R.fail("添加失败");
        }
    }

    @DeleteMapping("/delete")
    public R deleteByPrimaryKey(Long id){
        int i = userService.deleteByPrimaryKey(id);
        if (i>0){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    @PutMapping("/updateuser")
    public R updateByPrimaryKey(@RequestBody User user){
        User user1 = userService.updateByPrimaryKey(user);
        if (user1.getId()>0){
            return R.ok(user1);
        }else {
            return R.fail("修改失败");
        }
    }
    @GetMapping("/{id}")
    public R selectByPrimaryKey(@PathVariable Long id){
       User user = userService.selectByPrimaryKey(id);
       if (user != null){
           return R.ok(user);
       }       else {
           return R.fail("查询失败");
       }
    }
    @PutMapping("/selective")
    public R updateByPrimaryKeySelective(@RequestBody User user){
        int i = userService.updateByPrimaryKeySelective(user);
        if (i>0){
            return R.ok("有选择地修改成功");
        }else {
            return R.fail("有选择地修改失败");
        }
    }
}
